const express = require('express');
const app = express();
const ipList = require('./ipconfig.js');
const cors = require('cors');
const axios = require('axios');
const path = require('path');

app.use(cors())
app.use(express.json())
app.use(express.static(path.join(__dirname, "dist"))); // 静态文件index.html
app.use(express.static("accets"));
// 定义GET API
app.get('/api/iplist', (req, res) => {
    let ip = getAgvStatus()

    res.json({ data: ip });
});

// 请求对应车辆，查询是否在线
function getAgvStatus() {
    let setIpList = [...ipList];
    setIpList.forEach(async(item) => {
        try {
            try{
                let res = await axios.get(`http://${item.ip}:3333/api/ip`)
                console.log("26---", item.ip,res.data.isOnline);
                if (res.data.isOnline) {
                    // 在当前数据上添加字段isOnline: true
                    item.isOnline = "WiFi在线";
                }
            } catch (error) {
            }
            try{
                let res5 = await axios.get(`http://${item.ip5}:3333/api/ip`)
                console.log("35---", item.ip, res5.data.isOnline);
                if (res5.data.isOnline) {
                    // 在当前数据上添加字段isOnline: true
                    item.isOnline5 = "5G在线";
                }
            } catch (error) {
            }
            console.log("34---",setIpList);
        } catch (error) {
        }
        
    })
    return setIpList;
}
getAgvStatus()


const port = 3000;
// 启动服务
app.listen(port, () => {
    console.log(`Server is running on http://localhost:${port}`);
});
